package com.ak.server.platform.controller;

import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;
import com.ak.common.entity.AKResponse;
import com.ak.common.enums.Enums;
import com.ak.server.platform.entity.DictItem;
import com.ak.server.platform.service.IDictItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @Description
 * @Author Created by Vean
 */
@Api(tags = "字典详情模块")
@RestController
@RequestMapping("dictItem")
@RequiredArgsConstructor
public class DictItemController extends BaseController {

    private final IDictItemService dictItemService;

    /**
     * 分页查询字典详情内容
     *
     * @param page        分页对象
     * @param dictItem
     * @return
     */
    @ApiOperation("查询字典详情集合")
    @LogEndpoint(operation = "查询字典详情集合")
    @GetMapping
    public AKResponse getDictItemPage(Page<DictItem> page, DictItem dictItem) {
        dictItem.setDelFlag(Enums.DelFlag.EXIST.getCode());
        return new AKResponse().data(dictItemService.page(page, Wrappers.query(dictItem)));
    }

    /**
     * @param dictItem
     * @return
     */
    @ApiOperation("添加字典详情")
    @LogEndpoint(operation = "添加字典详情")
    @PreAuthorize("hasAuthority('dictItem:add')")
    @PostMapping
    public AKResponse add(@Valid DictItem dictItem) {
        dictItem.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return new AKResponse().data(dictItemService.save(dictItem));
    }

    /**
     * @param dictItem
     * @return
     */
    @ApiOperation("更新字典详情")
    @LogEndpoint(operation = "更新字典详情")
    @PreAuthorize("hasAuthority('dictItem:update')")
    @PutMapping
    public AKResponse update(@Valid DictItem dictItem) {
        dictItem.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        return new AKResponse().data(dictItemService.updateById(dictItem));
    }

    /**
     * @param id
     * @return
     */
    @ApiOperation("删除字典详情")
    @LogEndpoint(operation = "删除字典详情")
    @PreAuthorize("hasAuthority('dictItem:delete')")
    @DeleteMapping("/{id}")
    public AKResponse delete(@PathVariable("id") String id) {
        DictItem dictItem = new DictItem();
        dictItem.setId(id).setDelFlag(Enums.DelFlag.DEL.getCode());
        return new AKResponse().data(dictItemService.updateById(dictItem));
    }


}
